//
//  OrderColumnView.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/22.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "OrderColumnView.h"

@interface OrderColumnView()

@property (nonatomic,strong) UIScrollView *scrollView;

///标题数组
@property (nonatomic,strong) NSArray *titleArr;

///滑动条V
@property (nonatomic,strong) UIView *sliderV;

///标题数组
@property (nonatomic,strong) NSMutableArray *btnsArr;


@end

@implementation OrderColumnView

- (instancetype)initWithFrame:(CGRect)frame withTitles:(NSArray *)titles{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        self.titleArr = titles;
        self.btnsArr = [NSMutableArray array];
        
        [self initUI];
    }
    return self;
}


#pragma mark - setter
- (void)setSourceType:(NSInteger)sourceType{
    _sourceType = sourceType;
    
    for (UIButton *btn in self.btnsArr) {
        [btn setTitleColor:MainPureColor(85) forState:normal];
        [btn.titleLabel setFont:FontSet(14)];
        if (btn.tag == _sourceType) {
            [btn setTitleColor:MainBlackTextColor forState:normal];
            [btn.titleLabel setFont:FontSetWithWeight(15, 0.3f)];
        }
    }
    
    self.sliderV.left = _sourceType*(ScreenWidth/6) + (ScreenWidth/6/2-7);
}

#pragma mark - UI
-(void)initUI{
    
    [self addSubview:self.scrollView];
    
    for (int i = 0; i<self.titleArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(5 + i*(ScreenWidth/6), 10, ScreenWidth/6-10, 36);
        [btn setTitle:self.titleArr[i] forState:normal];
        [btn setTitleColor:MainPureColor(85) forState:normal];
        [btn.titleLabel setFont:FontSet(14)];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        if (i == 0) {
            [btn setTitleColor:MainBlackTextColor forState:normal];
            [btn.titleLabel setFont:FontSetWithWeight(15, 0.3f)];
        }
        [self.scrollView addSubview:btn];
        [self.btnsArr addObject:btn];
    }
    
    [self.scrollView addSubview:self.sliderV];
}

#pragma mark - lazy
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 56)];
        _scrollView.pagingEnabled = NO;
        _scrollView.bounces = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.backgroundColor = MainBackColor;
    }
    return _scrollView;
}

- (UIView *)sliderV{
    if (!_sliderV) {
        _sliderV = [[UIView alloc] initWithFrame:CGRectMake((ScreenWidth/6/2)-7, 52, 14, 2)];
        _sliderV.backgroundColor = RGB(26, 128, 255);
        _sliderV.clipsToBounds = YES;
        _sliderV.layer.cornerRadius = 1;
    }
    return _sliderV;
}


#pragma mark - SEL
-(void)btnClick:(UIButton *)btn{
    
    for (UIButton *btn1 in self.btnsArr) {
        [btn1 setTitleColor:MainPureColor(85) forState:normal];
        [btn1.titleLabel setFont:FontSet(14)];
    }
    [btn setTitleColor:MainBlackTextColor forState:normal];
    [btn.titleLabel setFont:FontSetWithWeight(15, 0.3f)];
    
    self.sliderV.left = btn.tag*(ScreenWidth/6) + (ScreenWidth/6/2-7);
    
    NSInteger index = (NSInteger)btn.tag;
    
    if (self.touchBlock) {
        self.touchBlock(index);
    }
}


@end
